﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Resources;
using System.IO;
using System.Reflection;

namespace Common.Utilities.TextMatch
{
    /// <summary>
    /// 
    /// </summary>
    public class StopWordsUtil
    {
        /// <summary></summary>
        public enum StopWordList
        {
            /// <summary></summary>
            DefaultStopWordList,
            /// <summary></summary>
            PubmedStopWordList
        }
        private readonly StopWordList _StopWordList;

        private readonly List<string> _StopWords;
        /// <summary>
        /// all lower case
        /// </summary>
        public List<string> StopWords
        {
            get { return _StopWords; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stopWordList"></param>
        public StopWordsUtil(StopWordList stopWordList)
        {
            this._StopWordList = stopWordList;
            this._StopWords = new List<string>();
            ResourceManager resMgr =
                    new ResourceManager(
                        this.GetType().Namespace + ".StopWords",
                        Assembly.GetAssembly(this.GetType()));
            string allStopWords = resMgr.GetString(this._StopWordList.ToString());
            try
            {
                string[] words = allStopWords.Split(new char[] {'\n','\r'});
                foreach(string word in words)
                {
                    if(word.Trim().Length>0 && !this._StopWords.Contains(word.Trim()))
                        this._StopWords.Add(word.Trim());
                }
            }
            catch (Exception)
            { }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="word"></param>
        /// <returns></returns>
        public bool IsStopWord(string word)
        {
            if (string.IsNullOrEmpty(word))
                return true;

            if (this._StopWords.Contains(word.ToLower().Trim()))
                return true;
            else
                return false;
        }
    }
}
